/**
 * SpringBoot_HelloWorld/ender.springBootStudy.springBoot_helloWorld.controller/HelloWorldController.java
 */
package ender.springBootStudy.springBoot_helloWorld.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ender.springBootStudy.springBoot_helloWorld.entity.Persion;
import ender.utils.StringUtil;

/**
 * @author chenxiwen
 * @date 2017年6月22日下午3:21:25
 */
@RestController  //@RestController = @Controller+@ResponseBody, 用来标识这是一个处理Restful请求的controller
@RequestMapping("/springBootStudy/helloWorld")  //如果在Class上加了@RequestMapping则相当于为本类中的所有映射路径前面多加了一层
public class HelloWorldController {
    @Value("${defaultName:HelloWorldController}")  //@Value注解用来注入application.properties或application.yml中的配置
    private String name;   
    
    @Value("${defaultMessage}")
    private String message;
    
    @Value("${rdm.uuid}")
    private String uuid;
    
    @Value("${rdm.number}")  //也可以直接使用@Value("${random.*}") 
    private int randomNum;
    
    @Autowired  //会通过配置文件自动装配
    private Persion author;
    
    @RequestMapping(value="/", method=RequestMethod.GET)  //路径中大小写敏感, 如果不指定method则所有类型的请求都可以映射到
    public String sayHelloWorld(){
        return (StringUtil.isEmpty(message)?"Hello World!":randomNum+" - "+uuid+" - "+message)+" - "+author.toString();
    }
    
    @GetMapping(value="/hello")  //这个写法等同于@RequestMapping(value="/hello", method=RequestMethod.GET)， value和method为联合主键，不可重复
    public String sayHelloWorld(@RequestParam(value = "name", required = false, defaultValue = "get") String userName){  //@RequestParam 不写value的话默认使用参数的名字
        return "Hello "+userName+"!";
    }
    
    //put 传递参数的时候也需要使用？key=value的形式，同get
    //或者x-www-form-urlencoded其实也是拼成key=value&key=value的形式（"Content-Type":"application/x-www-form-urlencoded"）
    //如果url中存在key=value1,同时在body中也存在key=value2且"Content-Type":"application/x-www-form-urlencoded" 则接受到的是value1,value2
    @PutMapping(value="/hello_put")  
    public String sayHelloWorldPut(@RequestParam(value = "name", required = false, defaultValue = "put") String userName){
        return "Hello "+userName+"!";
    }
    
    @DeleteMapping(value="/hello_del")//delete 只能和get一样使用？key=value的方式传递参数
    public String sayHelloWorldDel(@RequestParam(value = "name", required = false, defaultValue = "delete") String userName){
        return "Hello "+userName+"!";
    }
    
    @PostMapping(value="/hello_post")//如果url中存在key=value1,同时在body中也存在key=value2 则接受到的是value1,value2
    public String sayHelloWorldPost(@RequestParam(value = "name", required = false, defaultValue = "post") String userName){
        return "Hello "+userName+"!";
    }
    
}
